package com.hainatong.shiphelper.module.warehouse.viewmodel;


import java.util.List;

import com.hainatong.shiphelper.api.ApiResult;
import com.hainatong.shiphelper.api.ApiService;
import com.hainatong.shiphelper.api.VMObserver;
import com.hainatong.shiphelper.module.warehouse.entity.ImpaBean;
import com.hainatong.shiphelper.module.warehouse.entity.RukuParamBean;

import com.hainatong.shiphelper.module.warehouse.entity.WareHouseWuzhiBean;
import com.hainatong.mvvmbase.base.BaseViewModel;
import com.hainatong.mvvmbase.base.LiveResult;
import com.hainatong.mvvmbase.http.RxSchedulersUtils;

import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;
import io.reactivex.disposables.Disposable;

public class RuKuViewModel extends BaseViewModel
{

    public RuKuViewModel(@NonNull Application application)
    {
        super(application);
    }


    private MutableLiveData<LiveResult<ImpaBean>> impaResult = new MutableLiveData<>();

    public MutableLiveData<LiveResult<ImpaBean>> getImpaResult()
    {
        return impaResult;
    }

    /**
     * 查 物资
     * @param code
     */
    public void queryImpaByCode(String code)
    {
        if( code.length() > 35 )
        {
            return;
        }
        ApiService.Builder.getChanBoServer()
                          .getImpaByCode(code)
                          .compose(RxSchedulersUtils.Obs_io_main())
                          .subscribe(new VMObserver<ApiResult<ImpaBean>>() {
                              @Override
                              public void onStart(Disposable d)
                              {
                                addDisposable( d );
                              }

                              @Override
                              public void onError(int code, String errorMsg)
                              {
                                  getImpaResult().postValue( new LiveResult<>( false ,errorMsg ) );
                              }

                              @Override
                              public void onSuccess(ApiResult<ImpaBean> result)
                              {
                                  if( result.isOk() )
                                  {
                                      if( result.getData() != null )
                                      {
                                          getImpaResult().postValue( new LiveResult<>( true , result.getData() ) );
                                      }
                                      else
                                      {
                                          getImpaResult().postValue( new LiveResult<>( false ,result.getMessage() ) );
                                      }
                                  }
                                  else
                                  {
                                      getImpaResult().postValue( new LiveResult<>( false ,result.getMessage() ) );
                                  }
                              }
                          });

    }


    private MutableLiveData<LiveResult<List<String>>> impaBatchResult = new MutableLiveData<>();

    public MutableLiveData<LiveResult<List<String>>> getImpaBatchResult()
    {
        return impaBatchResult;
    }

    /**
     * 查 批次
     * @param code
     */
    public void queryImpaPici(String code)
    {

        ApiService.Builder.getChanBoServer()
                          .getImpaBatchNoByCode( code )
                          .compose( RxSchedulersUtils.Obs_io_main() )
                          .subscribe(new VMObserver<ApiResult<List<String>>>()
                          {
                              @Override
                              public void onStart(Disposable d)
                              {
                                addDisposable(d );
                              }

                              @Override
                              public void onError(int code, String errorMsg)
                              {
                                  getImpaBatchResult().postValue( new LiveResult<>( false ,errorMsg ) );
                              }

                              @Override
                              public void onSuccess(ApiResult<List<String>> result)
                              {
                                  if( result.isOk() )
                                  {
                                      getImpaBatchResult().postValue( new LiveResult<>( true,result.getData() ) );
                                  }
                                  else
                                  {
                                      getImpaBatchResult().postValue( new LiveResult<>( false,result.getMessage() ) );
                                  }
                              }
                          });

    }




    private MutableLiveData<LiveResult<RukuParamBean>> rukuResult = new MutableLiveData<>();

    public MutableLiveData<LiveResult<RukuParamBean>> getRukuResult()
    {
        return rukuResult;
    }

    /**
     *  物资 入库
     * @param mParam
     */
    public void rukuTask(RukuParamBean mParam)
    {
        //mParam.put(Constants.Key.TOKEN, GlobalConfig.getInstance().getToken());
        //mParam.put(Constants.Key.PAGE, String.valueOf( mPage ));
        //mParam.put(Constants.Key.PAGE_SIZE,  ""+Constants.DEF_PAGE_SIZE );

        // 出入库类别,1入库,2出库,3归还,4损坏,5消耗
        //"type",
        mParam.setType("1");

        ApiService.Builder.getChanBoServer()
                          .rukuTask( mParam )
                          .compose( RxSchedulersUtils.Obs_io_main() )
                          .subscribe(new VMObserver<ApiResult<RukuParamBean>>()
                          {
                              @Override
                              public void onStart(Disposable d)
                              {
                                  addDisposable( d );
                              }

                              @Override
                              public void onError(int code, String errorMsg)
                              {
                                  getRukuResult().postValue( new LiveResult<>( false ,errorMsg ) );
                              }

                              @Override
                              public void onSuccess(ApiResult<RukuParamBean> result)
                              {
                                  //{"data":{"impa_code":"874639","impa_id":"3957","type":"1","warehouse_id":null,"count":5.0,"produced_date":null,"indate":0,"enddate":null,"charge_user_id":"002938","charge_user":"test2","ship_id":null,"ship_name":null,"company_id":null,"company":null,"remark":null,"employer_id":null,"employer":null,"file_id":null,"createDate":"2021-04-01 09:58:32","createID":"74d48e19-3e7c-4a00-ab82-2f3c5187d370","creator":"test2","batchNo":"20210331","original_uuid":null,"supplier":null,"status":null,"supplier_id":null,"uuid":"cc26bde9-a275-408d-a365-886e4072288f"},"status":true,"code":"200","message":"操作成功"}
                                  if( result.isOk() )
                                  {
                                      getRukuResult().postValue( new LiveResult<>( true , result.getData() ) );
                                  }
                                  else
                                  {
                                      getRukuResult().postValue( new LiveResult<>( false , result.getMessage() ) );
                                  }
                              }
                          });

    }


    private MutableLiveData<LiveResult<Object>> chukuImpaResult = new MutableLiveData<>();

    public MutableLiveData<LiveResult<Object>> getChukuImpaResult()
    {
        return chukuImpaResult;
    }

    /**
     *  查询 入库的 物资 信息
     * @param impaCode
     */
    public void getChuKuImpaByCode(String impaCode)
    {
        ApiService.Builder.getChanBoServer()
                          .getWareWuzhiByImpaCode( impaCode )
                          .compose( RxSchedulersUtils.Obs_io_main() )
                          .subscribe(new VMObserver<ApiResult<Object>>()
                          {
                              @Override
                              public void onStart(Disposable d)
                              {
                                  addDisposable( d );
                              }

                              @Override
                              public void onError(int code, String errorMsg)
                              {
                                  getChukuImpaResult().postValue( new LiveResult<>( false ,errorMsg ) );
                              }

                              @Override
                              public void onSuccess(ApiResult<Object> result)
                              {
                                  if( result.isOk() )
                                  {
                                      getChukuImpaResult().postValue( new LiveResult<>( true , result.getData() ) );
                                  }
                                  else
                                  {
                                      getChukuImpaResult().postValue( new LiveResult<>( false , result.getMessage() ) );
                                  }
                              }
                          });


    }

    private MutableLiveData<LiveResult<List<WareHouseWuzhiBean>>> wareResult = new MutableLiveData<>();

    public MutableLiveData<LiveResult<List<WareHouseWuzhiBean>>> getWareResult() {
        return wareResult;
    }

    /**
     *  查 仓库
     * @param impaCode
     */
    public void queryWareHouseByImpaCode(String impaCode)
    {
        ApiService.Builder.getChanBoServer()
                          .getWareByImpaCode( impaCode )
                          .compose( RxSchedulersUtils.Obs_io_main() )
                          .subscribe(new VMObserver<ApiResult<List<WareHouseWuzhiBean>>>()
                          {
                              @Override
                              public void onStart(Disposable d)
                              {
                                 addDisposable( d );
                              }

                              @Override
                              public void onError(int code, String errorMsg)
                              {
                                  getWareResult().postValue( new LiveResult<>( false,errorMsg ) );
                              }

                              @Override
                              public void onSuccess(ApiResult<List<WareHouseWuzhiBean>> result)
                              {
                                  if( result.isOk() )
                                  {
                                      getWareResult().postValue( new LiveResult<>( true, result.getData() ) );
                                  }
                                  else
                                  {
                                      getWareResult().postValue( new LiveResult<>( false,result.getMessage()  ) );
                                  }
                              }
                          });
    }

}
